﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using QuocTeViet.DO;
using QuocTeViet.BL;

namespace QuocTeViet.AdminCP.PageUC
{
    public partial class ucProductCategory : System.Web.UI.UserControl
    {
        cmsLoaiSanPhamDO objLSanPham = new cmsLoaiSanPhamDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["PID"] != null)
            {
                objLSanPham.LoaiSanPhamID = int.Parse(Request.QueryString["PID"].ToString());
                InitForm();
                if (!IsPostBack)
                {
                    Ultility.ddlDatabinder(ddlDanhMucCha, cmsLoaiSanPhamDO.LOAISANPHAMID_FIELD, cmsLoaiSanPhamDO.TENLOAISANPHAM_FIELD, new DataView(new cmsLoaiSanPhamBL().SelectAll(0), "LoaiSanPhamID <> " + int.Parse(Request.QueryString["PID"].ToString()), "", DataViewRowState.CurrentRows));
                }
            }
            else
            {
                Ultility.ddlDatabinder(ddlDanhMucCha, cmsLoaiSanPhamDO.LOAISANPHAMID_FIELD, cmsLoaiSanPhamDO.TENLOAISANPHAM_FIELD, new cmsLoaiSanPhamBL().SelectAll(0));
            }
        }

        protected void InitForm()
        {
            objLSanPham = new cmsLoaiSanPhamBL().Select(objLSanPham);
            txtTenLSanPham.Text = objLSanPham.TenLoaiSanPham.ToString();
            txteMoTa.Text = objLSanPham.MoTa;
            txteTenLSanPham.Text = objLSanPham.eTenSanPham;
            txtOrderID.Text = objLSanPham.OrderID.ToString();
            if (objLSanPham.Images != null)
            {
                imImages.ImageUrl = "~/Media/ProductCate/" + objLSanPham.Images;
                imImages.Visible = true;
                check_Del_img.Visible = true;
            }
            else
            {
                imImages.ImageUrl = "~/image/images.jpg";
                imImages.Visible = true;
            }
            try
            {
                ddlDanhMucCha.SelectedValue = objLSanPham.ParentID.ToString();
            }
            catch
            {
                
                throw;
            }
            txteMoTa.Text = objLSanPham.eMoTa;
        }

        protected void SetObject()
        {
           // cmsLoaiSanPhamDO obj = new cmsLoaiSanPhamDO();
            objLSanPham.TenLoaiSanPham = txtTenLSanPham.Text;
            //objLSanPham.MoTa = txtMoTa.Text;
            objLSanPham.eMoTa = txteMoTa.Text;
            objLSanPham.eTenSanPham = txteTenLSanPham.Text;
            objLSanPham.ParentID = int.Parse(ddlDanhMucCha.SelectedValue);
            objLSanPham.OrderID = int.Parse(txtOrderID.Text.Trim());

          //  return obj;
        }
        cmsLoaiSanPhamBL BL = new cmsLoaiSanPhamBL();
        long i;
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            SetObject();
            if (objLSanPham.LoaiSanPhamID <= 0)
            {
                if (fileUploadImage.HasFile)
                {
                    //objLSanPham.Images = Ultility.CopyFile(fileUploadImage.PostedFile, "~/Media/ProductCate", objLSanPham.TenLoaiSanPham);
                    string FileName = string.Format("{0}{1}", fileUploadImage.FileName.Substring(0, fileUploadImage.FileName.LastIndexOf(".")) + "-" + DateTime.Now.ToString("dd-MM-yyyy-hhmmss"), fileUploadImage.FileName.Substring(fileUploadImage.FileName.LastIndexOf(".")));
                    objLSanPham.Images = FileName;
                    string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/ProductCate"), FileName);
                    fileUploadImage.SaveAs(SaveLocation);
                }

                i = new cmsLoaiSanPhamBL().Insert(objLSanPham);
                if (i > 0)
                    Functions.Alert("Thêm mới thành công", "Default.aspx?Page=ListProductCategory");
                else
                {
                    Functions.Alert("Lỗi!", Request.Url.AbsolutePath);
                }
            }
            else
            {
                if (fileUploadImage.HasFile && !check_Del_img.Checked)
                {
                    //objLSanPham.Images = Ultility.CopyFile(fileUploadImage.PostedFile, "~/Media/ProductCate", objLSanPham.TenLoaiSanPham);
                    string FileName = string.Format("{0}{1}", fileUploadImage.FileName.Substring(0, fileUploadImage.FileName.LastIndexOf(".")) + "-" + DateTime.Now.ToString("dd-MM-yyyy-hhmmss"), fileUploadImage.FileName.Substring(fileUploadImage.FileName.LastIndexOf(".")));
                    objLSanPham.Images = FileName;
                    string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/ProductCate"), FileName);
                    fileUploadImage.SaveAs(SaveLocation);
                }
                //Delete image
                if (check_Del_img.Checked)
                {
                    Ultility.DeleteFile("~/Media/ProductCate", objLSanPham.Images);
                    objLSanPham.Images = null;
                }

                BL.Update(objLSanPham);
                Functions.Alert("Cập nhật thành công", "Default.aspx?Page=ListProductCategory");
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {

        }
    }
}